﻿// -----------------------------------------------------------------------
// <copyright file="Invoice_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using PMSGSC.Common;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Invoice_DAL:BaseDAL
    {
      
        
        // Lấy tất cả Invoice
        public static List<Invoice> GetAll()
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Invoices);
            return DB.Invoices.ToList();
        }
        public static List<Invoice> GetIOList(DateTime From, DateTime To, int componentID, int Type, int shipID)
        {
            var predicate = PredicateBuilder.True<Invoice>();
            predicate = predicate.And(n => n.Ship_ID == shipID);
            predicate = predicate.And(n => n.InvoiceDate.Value.Date.CompareTo(From) >= 0);
            predicate = predicate.And(n => n.InvoiceDate.Value.Date.CompareTo(To) <= 0);
            if (componentID != 0)
                predicate = predicate.And(n => n.Equipment_ID == componentID);
            if (Type == 1)
                predicate = predicate.And(n => n.Type == true);
            if(Type == 2)
                predicate = predicate.And(n => n.Type == false);

            return DB.Invoices.Where(predicate).Cast<Invoice>().ToList();
        }
        public static Invoice GetNewInvoice()
        {
            return DB.Invoices.ToArray().LastOrDefault();
        }
        // Lấy Invoice theo Id
        public static Invoice GetDataById(int id)
        {
            return DB.Invoices.Where(n => n.Invoice_ID == id).FirstOrDefault();
        }
        //Thêm một Invoice
        public static bool Insert(Invoice Invoice)
        {
            try
            {
                DB.Invoices.InsertOnSubmit(Invoice);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một Invoice
        public static bool Delete(Invoice Invoice)
        {
            try
            {
                DB.Invoices.DeleteOnSubmit(Invoice);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật Invoice
        public static bool Update(Invoice Invoice)
        {
            try
            {
                //  var result = DB.Stocks.Single(c => c.Stock_ID == Invoice.Stock_ID);
                //  DB.Invoices.First().Stock = result;
                //  DB.Invoices.First().Employee_ID = Invoice.Employee_ID;
                //  DB.Invoices.First().ImportStock_ID = Invoice.ImportStock_ID;
                //  DB.Invoices.First().IEType = Invoice.IEType;
                //  DB.Invoices.First().InvoiceDate = Invoice.InvoiceDate;
                //  DB.Invoices.First().InvoiceCode = Invoice.InvoiceCode;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static int GetOrderNumberVoucherOut()
        {
            return DB.Invoices.Where(n=>n.Type == false).Count() + 1;
        }
        public static int GetOrderNumberVoucherIn()
        {
            return DB.Invoices.Where(n => n.Type == true).Count() + 1;
        }
    }
}
